//
//  GDViewController.m
//  GDWeiboNJ
//
//  Created by zhouguodong on 15/4/3.
//  Copyright (c) 2015年 shuanyuan. All rights reserved.
//

#import "GDViewController.h"
#import "GDTabBar2.h"
#import "HomeController.h"
#import "DiscoveryController.h"
#import "ProfileController.h"
#import "MessageController.h"

@interface GDViewController ()<GDTabBarDelegate>
// 自定义tabbar属性
@property (weak,nonatomic)GDTabBar2 *customTabbar;
@end

@implementation GDViewController

- (instancetype)init
{
    if(self = [super init])
    {
    
    }
        return self;
}
//init 方法内部会调用initwithNIbName方法,所以在自定义控制器时,可以把初始化的代码放到initWithNibName方法中,以后无论是通过init方法还是通过initwithnibName方法,都可以初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        UIViewController *home = [self addControllerWithClass:[HomeController class] title:@"首页" image:@"tabbar_home" selectImgae:@"tabbar_home_highlighted"];
        UIViewController *message = [self addControllerWithClass:[MessageController class] title:@"消息" image:@"tabbar_message_center" selectImgae:@"tabbar_message_center_highlighted"];
        UIViewController *discover = [self addControllerWithClass:[DiscoveryController class] title:@"发现" image:@"tabbar_discover" selectImgae:@"tabbar_discover_highlighted"];
        UIViewController *profile = [self addControllerWithClass:[ProfileController class] title:@"我" image:@"tabbar_profile" selectImgae:@"tabbar_profile_highlighted"];
//        self.viewControllers = @[home,message,discover,profile];
    }
    return self;
    
}
// 根据一个类创建控制器
- (UIViewController *)addControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)image selectImgae:(NSString *)selectedImage
{
    // 注意:不是所有的控制器都是通过init方法初始化的
    UIViewController *vc = [[class alloc]init];
    vc = [self addControllerWithVC:vc title:title image:image selectImgae:selectedImage];

    return vc;
}
// 根据一个创建好的控制器,返回初始化控制器
- (UIViewController *)addControllerWithVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectImgae:(NSString *)selectedImage
{
    vc.view.backgroundColor = GDARcolor;
//    vc.tabBarItem.title = title;
//    // 设置导航条的标题
//    vc.navigationItem.title = title;
    vc.title = title;
    // 1.创建图片
    vc.tabBarItem.image = [UIImage imageNamed:image];
    // 2.ios7需要通过代码告诉系统不要用tabbar.tintcolor来渲染选中图片
    UIImage *newImage = [UIImage imageNamed:selectedImage];
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = newImage;
    
    // 设置选中标题不需要渲染
    // 设置文字的属性有一个规律:NSxxxAttrbuteName
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
    
    // 包装导航控制器
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    // 添加子控制器
    [self addChildViewController:nav];
    
    
    // 利用系统的item作为模型传递数据来,设置自定义tabbar的item
    [self.customTabbar addItem:vc.tabBarItem];
    
    
    return vc;
}

- (void)viewDidLoad
{
    // 1.创建自定义的tabbar
    GDTabBar2 *tabbar = [[GDTabBar2 alloc]init];
    
//    // 2.利用KVC方法替换系统的tabbar,KVC的实现原理基于运行时,它可以在运行时动态的修改属性
//    [self setValue:tabbar forKeyPath:@"tabBar"];
    
    // 设置代理
    tabbar.delegate = self;
    // 2.利用自定义的tabbar来创建
    tabbar.frame = self.tabBar.frame;
    [self.view addSubview:tabbar];
    // 3.绑定成员变量
    self.customTabbar = tabbar;
}

// 代理方法
- (void)tabBar:(GDTabBar2 *)tabBar selectedBtnfrom:(NSInteger)from to:(NSInteger)to
{
    DDLogDebug(@"from = %tu  to = %tu", from, to);
    // 切换控制器
    // 1.取出当前选中按钮对应的控制器
    //    UIViewController *vc = self.childViewControllers[to];
    //    self.selectedViewController = vc;
    
    self.selectedIndex = to;
}


@end
